import { useState } from "react"

export default function ref<T>(value: T) {
  interface Target {
    value: T
  }
  const [originValue, setOriginValue] = useState<T>(value)
  const target: Target = {
    value,
  }
  const proxyTarget: Target = new Proxy(target, {
    get() {
      return originValue
    },
    set(target: any, prop: string | symbol, newVal: any) {
      target[prop] = newVal
      setOriginValue(newVal)
      return true
    },
  })

  return proxyTarget
}
